package utility;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectIO {
	public static Object readFromFile(String fileName)
			throws FileNotFoundException, IOException, ClassNotFoundException {

		File f = new File(fileName).getAbsoluteFile();
		f.getParentFile().mkdirs();

		FileInputStream fs=new FileInputStream(f);
		BufferedInputStream bs= new BufferedInputStream(fs
				);
		ObjectInputStream is = new ObjectInputStream(bs);

		Object o=is.readObject();
		
		is.close();
		bs.close();
		fs.close();
		
		return o;
	}

	public static void writeToFile(Object o, String fileName)
			throws FileNotFoundException, IOException {
		File f = new File(fileName).getAbsoluteFile();
		f.getParentFile().mkdirs();

		FileOutputStream fs=new FileOutputStream(f);
		BufferedOutputStream bs= new BufferedOutputStream(fs
				);
		ObjectOutputStream os = new ObjectOutputStream(bs);

		os.writeObject(o);
		
		os.close();
		bs.close();
		fs.close();
		
	}
}
